package cn.iocoder.yudao.module.iot.controller.admin.phonemsglog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.iot.controller.admin.phonemsglog.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.phonemsglog.PhoneMsgLogDO;
import cn.iocoder.yudao.module.iot.service.phonemsglog.PhoneMsgLogService;

@Tag(name = "管理后台 - 短信和电话消息提醒记录")
@RestController
@RequestMapping("/iot/phoneMsglog")
@Validated
public class PhoneMsgLogController {

    @Resource
    private PhoneMsgLogService phoneMsgLogService;

    @PostMapping("/create")
    @Operation(summary = "创建短信和电话消息提醒记录")
    @PreAuthorize("@ss.hasPermission('iot:phone-msg-log:create')")
    public CommonResult<Integer> createPhoneMsgLog(@Valid @RequestBody PhoneMsgLogSaveReqVO createReqVO) {
        return success(phoneMsgLogService.createPhoneMsgLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新短信和电话消息提醒记录")
    @PreAuthorize("@ss.hasPermission('iot:phone-msg-log:update')")
    public CommonResult<Boolean> updatePhoneMsgLog(@Valid @RequestBody PhoneMsgLogSaveReqVO updateReqVO) {
        phoneMsgLogService.updatePhoneMsgLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除短信和电话消息提醒记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:phone-msg-log:delete')")
    public CommonResult<Boolean> deletePhoneMsgLog(@RequestParam("id") Integer id) {
        phoneMsgLogService.deletePhoneMsgLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短信和电话消息提醒记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:phone-msg-log:query')")
    public CommonResult<PhoneMsgLogRespVO> getPhoneMsgLog(@RequestParam("id") Integer id) {
        PhoneMsgLogDO phoneMsgLog = phoneMsgLogService.getPhoneMsgLog(id);
        return success(BeanUtils.toBean(phoneMsgLog, PhoneMsgLogRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得短信和电话消息提醒记录分页")
    //@PreAuthorize("@ss.hasPermission('iot:phone-msg-log:query')")
    public CommonResult<PageResult<PhoneMsgLogRespVO>> getPhoneMsgLogPage(@Valid PhoneMsgLogPageReqVO pageReqVO) {
        PageResult<PhoneMsgLogDO> pageResult = phoneMsgLogService.getPhoneMsgLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PhoneMsgLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短信和电话消息提醒记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:phone-msg-log:export')")
    @OperateLog(type = EXPORT)
    public void exportPhoneMsgLogExcel(@Valid PhoneMsgLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PhoneMsgLogDO> list = phoneMsgLogService.getPhoneMsgLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "短信和电话消息提醒记录.xls", "数据", PhoneMsgLogRespVO.class,
                        BeanUtils.toBean(list, PhoneMsgLogRespVO.class));
    }

}